<template>
  <div>
    <el-form :model="dataForm" ref="dataForm">
      <el-form-item>
        <el-button type="primary" @click="openSearch()">查询</el-button>
        <el-button @click="_export()" type="primary">导出</el-button>
<!--        <el-button type="danger"  @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <search ref="search" @onSearched="onSearch"/>
    <el-table
      :data="dataList"
      border
      style="width: 100%;"
      @selection-change="selectionChangeHandle"
    >
<!--      <el-table-column type="selection" align="center" width="50" />-->
      <el-table-column align="center" type="index" width="80" label="序号" />
      <el-table-column prop="deviceName" label="巡查设备" align="center" />
      <el-table-column prop="areaName" label="区域" align="center" />
      <el-table-column  prop="areaUserName" label="区域负责人" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.areaUserName !== ''">{{scope.row.areaUserName}}</span>
          <span v-else>~~</span>
        </template>
      </el-table-column>
      <el-table-column prop="auditStatus" label="审核状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.auditStatus === 0">未提交</el-tag>
          <el-tag v-if="scope.row.auditStatus === 1">审核中</el-tag>
          <el-tag v-if="scope.row.auditStatus === 2">通过</el-tag>
          <el-tag v-if="scope.row.auditStatus === 9">驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="审核时间" align="center" />
      <el-table-column prop="realname" label="上报人" align="center" />
      <el-table-column prop="createTime" label="上报时间" align="center" />
      <el-table-column prop="comments" label="情况说明" align="center">
        <template slot-scope="scope">
          <el-tooltip class="item" placement="top" effect="light">
            <div slot="content" style="width: 80px;">
              <span>{{scope.row.comments || '暂无数据'}}</span>
            </div>
            <el-tag v-if="scope.row.situationStatus === 1">设备良好</el-tag>
            <el-tag v-if="scope.row.situationStatus === 2">存在隐患</el-tag>
            <el-tag v-if="scope.row.situationStatus === 10">其他情况</el-tag>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="deviceStatus" label="设备状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.deviceStatus === 1">在用</el-tag>
          <el-tag v-if="scope.row.deviceStatus === 2">闲置</el-tag>
          <el-tag v-if="scope.row.deviceStatus === 3">待修</el-tag>
          <el-tag v-if="scope.row.deviceStatus === 4">借入</el-tag>
          <el-tag v-if="scope.row.deviceStatus === 5">借出</el-tag>
          <el-tag v-if="scope.row.deviceStatus === 6">报废</el-tag>
          <el-tag v-if="scope.row.deviceStatus === 7">其他</el-tag>
        </template>
      </el-table-column>
            <el-table-column fixed="right" align="center" width="150" label="操作">
            <template slot-scope="scope">
<!--              <el-button type="text" @click="addOrUpdate(scope.row.id)">详情</el-button>-->
<!--              v-if="scope.row.auditStatus === 0 || scope.row.auditStatus === 2"-->
              <el-button type="text"  @click="deleteHandle(scope.row.id)">删除</el-button>
<!--              <span v-else>审核通过可删除</span>-->
            </template>
          </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <patrol-add-or-update ref="patrolAddOrUpdate"></patrol-add-or-update>
  </div>
</template>

<script>
  import PatrolAddOrUpdate from './region/patrol-add-or-update'
  import Search from './region/search'
  import Theme from '../../common/theme'
  export default {
    name: 'patrol',
    data() {
      return {
        dataList: [],
        loading: false,
        dataForm: {
          deviceName: ''
        },
        pageIndex: 1,
        Writepathexe: '',
        totalPage: 0,
        startTime: '',
        endTime: '',
        dataListSelections: [],
        pageSize: 10
      }
    },
    components: { Theme, PatrolAddOrUpdate, Search },
    mounted() {
      this.getDataList()
    },
    methods: {
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      _export() {
        window.location = this.$http.downloadUrl('/device/patrol/record/exportDevicePatrolRecord')
      },
      openSearch () {
        this.$refs.search.open()
      },
      onSearch(searchForm) {
        this.pageIndex = 1
        this.totalCount = 0
        this.getDataList(searchForm)
      },
      async getDataList(searchForm = {}) {
        var requestData = Object.assign(searchForm, {
          page: this.pageIndex,
          limit: this.pageSize
        })
        var res = await this.$http.post('/device/patrol/record/page', requestData)
        // var res = await this.$http.post('/device/patrol/record/page', {
        //   deviceName: this.dataForm.deviceName,
        // })
        if (res.code === 200) {
          this.dataList = res.data.list
          this.totalPage = res.data.totalCount
        }
      },
      addOrUpdate(id) {
        this.$nextTick(() => this.$refs.patrolAddOrUpdate.init(id))
      },
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      async deleteHandle(id) {
        // var ids = id || this.dataListSelections.map(item => item.id).join(',')
        var confirm = await this.$confirm(`确定要进行删除操作吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        if (confirm !== 'confirm') return

        let res = await this.$http({
          url: '/device/patrol/record/delete',
          method: 'post',
          data: {
            id: id
          }
        })
        if (res && res.code === 200) {
          this.$notify.success({ title: '成功', message: '删除成功' })
          this.pageIndex > 1 && this.dataList.length <= 1 && this.pageIndex --
          this.getDataList()
        } else {
          this.$notify.error({ title: '错误', message: res.msg })
        }
      }
    }
  }
</script>

<style scoped>

</style>
